﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data;
using System.Configuration;
namespace ReportBusiness
{
    public class UserImage
    {
        string userImagesPath = ConfigurationManager.AppSettings["userImagesPath"];
        public DataTable getUserImage(DataTable dt, string colName, string username_digit8, string username_digit6)
        {
            string url = "";
            HttpWebRequest re = null;
            HttpWebResponse res = null;
            string errMsg = "";

            foreach (DataRow dr in dt.Rows)
            {
                //先查找8位
                try
                {
                    url = userImagesPath + dr[username_digit8].ToString() + ".JPG";
                    re = (HttpWebRequest)WebRequest.Create(url);
                    res = (HttpWebResponse)re.GetResponse();
                    if (res.ContentLength != 0)
                    {
                        dr[colName] = url;
                    }
                }
                catch (Exception ex)
                {
                    errMsg = ex.Message;
                }
                finally
                {
                    if (re != null) re.Abort();
                    if (res != null) res.Close();
                }

                //8位未找到再找6位
                if (dr[colName].ToString() == "" && dr[username_digit6].ToString() != "")
                {
                    try
                    {
                        url = userImagesPath + dr[username_digit6].ToString() + ".JPG";
                        re = (HttpWebRequest)WebRequest.Create(url);
                        res = (HttpWebResponse)re.GetResponse();
                        if (res.ContentLength != 0)
                        {
                            dr[colName] = url;
                        }
                    }
                    catch (Exception ex)
                    {
                        errMsg = ex.Message;
                    }
                    finally
                    {
                        if (re != null) re.Abort();
                        if (res != null) res.Close();
                    }
                }
            }
            return dt;
        }
    }
}
